package com.alibaba.middleware.hsf.guide.serial.hessian.hierarchy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * <pre>
 * 验证Java支持父子类型同名
 * 
 * 使用Java序列化到临时文件，然后加以读取，可以看到父子同名属性处理正常
 * </pre>
 * 
 * @author weipeng2k 2015年10月8日 下午4:56:43
 */
public class JavaSerial {
    
    static File file;
    
    @BeforeClass
    public static void createFile() {
        try {
            file = File.createTempFile("javaserial", "dat");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    @Test
    public void serial() throws Exception {
        Child child = new Child();
        child.setChildField("childValue");
        child.setField("fatherValue");
        
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
        out.writeObject(child);
        out.flush();
        out.close();
    }
    
    @Test
    public void deserial() throws Exception {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
        Child cl = (Child) in.readObject();
        Assert.assertNotNull(cl.getField());
        System.out.println(cl);
        in.close();
    }
    
    @AfterClass
    public static void removeFile() {
        file.delete();
    }
}
